package cn.momoshenchi.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.momoshenchi.product.entity.ProductAttrValueEntity;
import cn.momoshenchi.product.service.ProductAttrValueService;
import cn.momoshenchi.product.vo.AttrReVo;
import cn.momoshenchi.product.vo.AttrVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.momoshenchi.product.entity.AttrEntity;
import cn.momoshenchi.product.service.AttrService;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.R;



/**
 * 商品属性
 *
 * @author ${author}
 * @email ${email}
 * @date 2021-04-23 22:04:06
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 更新规格
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuId(@PathVariable Long spuId,
                         @RequestBody  List<ProductAttrValueEntity> entities){
       productAttrValueService.updateSpuId(spuId,entities);
        return R.ok();
    }
    /**
     * 查看规格
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listforspu(@PathVariable Long spuId){
   List<ProductAttrValueEntity>entities= productAttrValueService.baseAttrList(spuId);

        return R.ok().put("data",entities );
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/{attrType}/list/{categoryId}")
    public R baseAttr(@RequestParam Map<String, Object> params ,@PathVariable Long categoryId,
            @PathVariable String attrType)
    {
//        PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryBaseAttrPage(params,categoryId,attrType);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrReVo attr =attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
//		attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrReVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
